﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Agile_E
{
    public partial class Pending : System.Web.UI.Page
    {
        private static string vin;
        private static string empid;
        private static int customer_id;
        private static int dealer_id;
        private static PendingCar pCar = new PendingCar();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (User.Identity.IsAuthenticated)
                    txtname.Text = User.Identity.Name;
                empid = Session["Employee_ID"].ToString();
                dealer_id = Convert.ToInt32(Session["Dealership_ID"]);
                Employees thisGuy = new Employees();
                DatabaseHandler.connect();
                thisGuy = DatabaseHandler.getEmployeeByID(empid);
                Dealership thisDealer = new Dealership();
                thisDealer = DatabaseHandler.getDealershipbyID(dealer_id);
                txtempid.Text = empid;
                txtname.Text = thisGuy.Info.FirstName + " " + thisGuy.Info.MiddleName + " " + thisGuy.Info.LastName;
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                vin = txtvin.Text;
                customer_id = Convert.ToInt32(txtcust.Text);

                Car pendingCar = new Car();
                Customer buyer = new Customer();
                empid = txtempid.Text;
                DatabaseHandler.connect();
                pendingCar = DatabaseHandler.getCarbyVIN(vin);
                buyer = DatabaseHandler.getCustomer(customer_id);
                pendingCar.Status = 3;
                DatabaseHandler.updateCar(pendingCar);
                pCar.C_ID = customer_id;
                pCar.E_ID = Convert.ToInt32(empid);
                pCar.Price = txtprice.Text;
                pCar.Make = pendingCar.Make;
                pCar.Model = pendingCar.Model;
                pCar.VIN = txtvin.Text;
                pCar.Year = pendingCar.Year;
                pCar.Seller = Session["emp_name"].ToString();
                DatabaseHandler.carPending(pCar);
                
            }
            catch (Exception ex)
            {
                ((Main)this.Master).writeStatus(ex.Message, true);
            }
        }
    }
}
